$(function () {
    setInterval("get_notification_count()", 10000); //未读消
    get_notification_count();
    init();

})

//页面初始化
function init(name) {
    show_toolbar_bottom(name);
    int_textarea();
}

/*****div 模拟 textarea******/
function int_textarea() {
    if ($('.textarea').length > 0) {
        $('.textarea').each(function (i, v) {
            textarea_blur(this);
        })
        $('.textarea').focus(function () {
            _textarea_focus_(this);
        });

        $('.textarea').blur(function () {
            textarea_blur(this)
        });

        $('.textarea').click(function () {
            $(this).focus();
            //textarea_focus(this)
        });


    }
}

function _textarea_focus_(obj) {
    var html = $(obj).html();
    var placeholder = $(obj).attr("placeholder");
    if (html == placeholder) {
        $(obj).html("");
        $(obj).removeAttr("style");
    }

}
function textarea_blur(obj) {
    var html = $(obj).html();
    var placeholder = $(obj).attr("placeholder");
    if (html == "") {
        $(obj).html(placeholder);
        $(obj).attr("style", "color:#A8A8A8")
    }
}

/*****显示底部导航***/
function show_toolbar_bottom(name) {
    if (name == "" || typeof(name) == "undefined") {
        var name = $(".page").attr("data-page");
    }
    $(".toolbar_bottom").hide();
    var tabbar_html = $("#" + name + "_tabbar_html").html();
    $(".toolbar_bottom").html("");
    if (typeof(tabbar_html) !== "undefined") {
        $(".toolbar_bottom").html(tabbar_html);
        $(".toolbar_bottom").show();
    }
}
/***********赞*****/
function memu_good(obj) {
    var id = $(obj).attr("id");
    $.getJSON("/BoxApi/BoxSNS/Topic/goods", {
            topic_id: id
        },
        function (data) {
            if (data.error == 0) {
                $(".article_item_good_" + id).prepend('<a href="/u/' + user_info.user_id + '/" data-transition="none"><img src="' + user_info.headimg + '"></a>');
            } else if (data.error == 6) {
                dialog("您已经赞过了。");
            } else {
                dialog("很抱歉，没有操作成功。");
            }
        });

}

//评论一条分享
function add_comment(obj) {
    //if(!is_login())return false;
    var content = $("#messageText").html();
    var to_user_id = $("#to_user_id").val();
    var to_comment_id = $("#to_comment_id").val();
    var pid = $("#pid").val();
    var type = $("#type").val();
    if (content == "") {
        dialog('回复内容不能为空啊！！！！');
        return false;
    }
    $(obj).hide();
    $(".memupostBntHover").show();
    $.post("/BoxApi/BoxSNS/Comment/add", {
        content: content,
        to_user_id: to_user_id,
        to_comment_id: to_comment_id,
        pid: pid,
        type: type
    }, function (data) {
        data = JSON.parse(data);
        if (data.error > 0) {
            dialog(data.msg);
        } else {
            var pid = data.data.pid;
            data.data.del_act = 1;
            box_render({0: data.data}, "#commentTemplate", ".article_item_comment_" + pid, "prepend");
            dialog('发送成功', 500, function () {
                mainView.router.back();
            });

        }
    })
}
//删除一条帖子
function del_topic(obj) {
    var topic_id = $(obj).attr("topic_id");
    $.getJSON("/BoxApi/BoxSNS/topic/del", {topic_id: topic_id}, function (data) {
        if (data.error > 0) {
            dialog(data.msg);
        } else {
            dialog("删除成功", 500, function () {
                $(".one_memu_" + topic_id).fadeOut();
                $(".one_memu_" + topic_id).remove();
                //mainView.router.back();
            });
        }
    })
}
//删除一条评论
function del_comment(obj) {
    var comment_id = $(obj).attr("comment_id");
    var type = $(obj).attr("type");
    $.getJSON("/BoxApi/BoxSNS/Comment/del", {comment_id: comment_id, type: type}, function (data) {
        if (data.error > 0) {
            dialog(data.msg);
        } else {
            dialog("删除成功", function () {
                $(".memu_one_comment_" + comment_id).fadeOut();
                $(".memu_one_comment_" + comment_id).remove();
            });
        }
    })
}

//提醒数字
function get_notification_count() {
    user_id = user_info.user_id;
    $.getJSON("/BoxApi/BoxSNS/Noti/get_notification_count", {user_id: user_id}, function (data) {
        if (data.error == 0) {
            $.each(data.data, function (i, v) {
                var obj = $("#" + i + "_count");
                if (obj.length) {
                    if (v > 0) {
                        obj.html(v);
                        obj.show();
                    } else {
                        obj.html(0);
                        obj.hide();
                    }
                }
            })
        }
    })
}

/*******关注****/
function follow(obj) {
    var to_user_id = $(obj).attr("data-id");
    var act = $(obj).attr("act");
    var url = "/BoxApi/BoxSNS/Friends/" + act;
    var act_str = "follow";
    var act_text = "关注";
    if (act == "follow") {
        act_str = "del_follow";
        var act_text = "取消关注";
    }
    $.getJSON(url, {to_user_id: to_user_id}, function (data) {
        if (data.error == 0) {
            $(obj).attr("act", act_str);
            $(obj).html(act_text);
        } else {
            dialog(data.msg);
        }
    })
}

/*****读了一个提醒 ***/
function read_one_noti(id) {
    $.getJSON("/BoxApi/BoxSNS/Noti/read_one", {id: id});
}
/****保存用户信息之后的操作**/
function after_member_save(data) {
    if (data.error == 0) {
        dialog("信息保存成功");
    }
}

/**********发送信息**/
function sendmsg(obj) {
    var msgbox = $(obj).attr("msgbox");
    var content = $("#" + msgbox).val();
    var to_user_id = $("#" + msgbox).attr("to_user_id");
    if (content == "") {
        return false;
    }
    $.post("/BoxApi/BoxSNS/msg/send", {to_user_id: to_user_id, content: content}, function (data) {

    })
}


function textarea_focus(obj) {
}

